home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr49 / vesa18.zip / MODEDUMP.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  6KB  |  234 lines

  1. /* VESA package for emx/gcc --- Copyright (c) 1993 by Johannes Martin */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/hw.h>
  6.  
  7. #include "vesa.h"
  8. #include "vgaports.h"
  9.  
  10. /* VGA indexes max counts */
  11. #define CRT_C   24      /* 24 CRT Controller Registers */
  12. #define ATT_C   21      /* 21 Attribute Controller Registers */
  13. #define GRA_C   9       /* 9  Graphics Controller Registers */
  14. #define SEQ_C   5       /* 5  Sequencer Registers */
  15. #define MIS_C   1       /* 1  Misc Output Register */
  16. #define EXT_C   11      /* 10 SVGA Extended Registers */
  17. #define HIC_C   1       /* 1  Hicolor Register     */
  18.  
  19. /* VGA registers saving indexes */
  20. #define CRT     0               /* CRT Controller Registers start */
  21. #define ATT     CRT+CRT_C       /* Attribute Controller Registers start */
  22. #define GRA     ATT+ATT_C       /* Graphics Controller Registers start */
  23. #define SEQ     GRA+GRA_C       /* Sequencer Registers */
  24. #define MIS     SEQ+SEQ_C       /* General Registers */
  25. #define EXT     MIS+MIS_C       /* SVGA Extended Registers */
  26. #define HIC     EXT+EXT_C       /* Hicolor Register */
  27.  
  28. static int et4000_test(void)
  29. {
  30.   char new, old, val;
  31.   int  base;
  32.  
  33.   /* test for Tseng clues */
  34.   old = _inp8(0x3cd);
  35.   _outp8(0x3cd, 0x55);
  36.   new = _inp8(0x3cd);
  37.   _outp8(0x3cd, old);
  38.  
  39.   /* return false if not Tseng */
  40.   if (new != 0x55)
  41.     return 0;
  42.  
  43.   /* test for ET4000 clues */
  44.   if (_inp8(0x3cc) & 1)
  45.     base = 0x3d4;
  46.   else
  47.     base = 0x3b4;
  48.   _outp8(base, 0x33);
  49.   old = _inp8(base+1);
  50.   new = old ^ 0xf;
  51.   _outp8(base+1, new);
  52.   val = _inp8(base+1);
  53.   _outp8(base+1, old);
  54.  
  55.   /* return true if ET4000 */
  56.   return (val == new);
  57. }
  58.  
  59. static int getdac(void)
  60. {
  61.   _inp8(0x3c8);
  62.   _inp8(0x3c6);
  63.   _inp8(0x3c6);
  64.   _inp8(0x3c6);
  65.   _inp8(0x3c6);
  66.   return(_inp8(0x3c6));
  67. }
  68.  
  69. static void et4000_save_regs(VESACHAR regs[72])
  70. {
  71.   int i;
  72.  
  73.   /* save extended CRT registers */
  74.   for (i = 0; i < 6; i++)
  75.     {
  76.       _outp8(CRT_I, 0x32+i);
  77.       regs[EXT+i] = _inp8(CRT_D);
  78.     }
  79.  
  80.   /* save extended sequencer register */
  81.   _outp8(SEQ_I, 7);
  82.   regs[EXT+6] = _inp8(SEQ_D);
  83.  
  84.   /* save some other ET4000 specific registers */
  85.   regs[EXT+7] = _inp8(0x3c3);
  86.   regs[EXT+8] = _inp8(0x3cd);
  87.  
  88.   /* save extended attribute register */
  89.   _inp8(IS1_R);    /* reset flip flop */
  90.   _outp8(ATT_IW, 0x16);
  91.   regs[EXT+9] = _inp8(ATT_R);
  92.   _outp8(CRT_I, 0x3f);
  93.   regs[EXT+10] = _inp8(CRT_D);
  94. }
  95.  
  96. static void saveregs(VESACHAR regs[72])
  97. {
  98.   int i;
  99.  
  100.   /* save VGA registers */
  101.   for (i = 0; i < CRT_C; i++)
  102.     {
  103.       _outp8(CRT_I, i);
  104.       regs[CRT+i] = _inp8(CRT_D);
  105.     }
  106.   for (i = 0; i < ATT_C; i++)
  107.     {
  108.       _inp8(IS1_R);
  109.       _outp8(ATT_IW, i);
  110.       regs[ATT+i] = _inp8(ATT_R);
  111.     }
  112.   for (i = 0; i < GRA_C; i++)
  113.     {
  114.       _outp8(GRA_I, i);
  115.       regs[GRA+i] = _inp8(GRA_D);
  116.     }
  117.   for (i = 0; i < SEQ_C; i++)
  118.     {
  119.       _outp8(SEQ_I, i);
  120.       regs[SEQ+i] = _inp8(SEQ_D);
  121.     }
  122.   regs[MIS] = _inp8(MIS_R);
  123.  
  124.   et4000_save_regs(regs);
  125.   regs[71] = getdac();
  126. }
  127.  
  128. static VESABOOL dumpmode(FILE *file, VESAWORD mode)
  129. {
  130.   VESAMODEINFO info;
  131.   VESACHAR     regs[72];
  132.  
  133.   if (!VesaSetMode(mode))
  134.     {
  135.       fprintf(stderr, "cannot activate mode 0x%x\n", mode);
  136.       return(FALSE);
  137.     }
  138.   if (mode == 0x03)
  139.     {
  140.       memset(&info, 0, sizeof(info));
  141.       info.ModeAttributes       = 0xf;
  142.       info.WindowAAttributes    = 0x7;
  143.       info.WindowGranularity    = 64;
  144.       info.WindowSize           = 64;
  145.       info.BytesPerScanline     = 160;
  146.       info.Width                = 80;
  147.       info.Height               = 25;
  148.       info.CharacterWidth       = 8;
  149.       info.CharacterHeight      = 16;
  150.       info.NumberOfBitsPerPixel = 4;
  151.     }
  152.   else
  153.     {
  154.       if (!VesaGetModeInfo(mode, &info))
  155.         {
  156.           fprintf(stderr, "VesaGetModeInfo failed, mode = 0x%x\n", mode);
  157.           return(FALSE);
  158.         }
  159.       if ((info.ModeAttributes & 1) == 0)
  160.         {
  161.           fprintf(stderr, "mode 0x%x is said to be \"not supported\"\n", mode);
  162.           fprintf(stderr, "but in fact, it seems to be supported...\n");
  163.           info.ModeAttributes |= 1;
  164.         }
  165.     }
  166.   saveregs(regs);
  167.   if ((fwrite(&mode, sizeof(mode), 1, file) != 1) ||
  168.       (fwrite(&info, sizeof(info), 1, file) != 1) ||
  169.       (fwrite(regs,  sizeof(regs), 1, file) != 1))
  170.     {
  171.       VesaSetMode(0x03);
  172.       fprintf(stderr, "error writing vesamode.dat\n");
  173.       exit(1);
  174.     }
  175.   return(TRUE);
  176. }
  177.  
  178. void main(void)
  179. {
  180.   VESAWORD count;
  181.   VESAINFO info;
  182.   FILE     *file;
  183.  
  184.   _portaccess(ATT_IW, IS1_R);
  185.  
  186.   if (!et4000_test())
  187.     {
  188.       fprintf(stderr, "No ET4000 detected\n");
  189.       exit(1);
  190.     }
  191.   if (!VesaGetInfo(&info))
  192.     {
  193.       fprintf(stderr, "VesaGetInfo failed\n");
  194.       exit(1);
  195.     }
  196.   if ((file = fopen("vesamode.dat", "wb")) == NULL)
  197.     {
  198.       fprintf(stderr, "cannot create vesamode.dat\n");
  199.       exit(1);
  200.     }
  201.   count = 0;
  202.   if (fwrite(&count, sizeof(count), 1, file) != 1)
  203.     {
  204.       fprintf(stderr, "error writing vesamode.dat\n");
  205.       exit(1);
  206.     }
  207.   if (!dumpmode(file, 0x03) ||
  208.       !dumpmode(file, 0x12) ||
  209.       !dumpmode(file, 0x13))
  210.     {
  211.       fprintf(stderr, "fatal: could not activate standard modes\n");
  212.       exit(1);
  213.     }
  214.   count = 3;
  215.   while (*info.Modes != 0xffff)
  216.     {
  217.       if ((*info.Modes != 0x03) &&
  218.           (*info.Modes != 0x12) &&
  219.           (*info.Modes != 0x13) &&
  220.           dumpmode(file, *info.Modes))
  221.         count++;
  222.       info.Modes++;
  223.     }
  224.   VesaSetMode(0x03);
  225.   fseek(file, 0, SEEK_SET);
  226.   if (fwrite(&count, sizeof(count), 1, file) != 1)
  227.     {
  228.       fprintf(stderr, "error writing vesamode.dat\n");
  229.       exit(1);
  230.     }
  231.   fclose(file);
  232.   exit(0);
  233. }
  234.